package com.auoem.radio.common.custview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.auoem.radio.common.utils.ResourceUtils;
import com.auoem.radio.databinding.FragmentConfirmDialogBinding;

public class ConfirmDialogFragment extends DialogFragment {

    private final static int MAX_LEN = 200;

    private FragmentConfirmDialogBinding mBinding;

    private OnConfirmListener mOnConfirmListener;

    private String mContent = "";

    public ConfirmDialogFragment() {
    }

    public ConfirmDialogFragment(@NonNull String content, OnConfirmListener onConfirmListener) {
        mContent = content;
        setOnConfirmListener(onConfirmListener);
    }

    public ConfirmDialogFragment(@NonNull int content, OnConfirmListener onConfirmListener) {
        if (content != 0) {
            mContent = ResourceUtils.getResourcesString(content);
        }
        setOnConfirmListener(onConfirmListener);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentConfirmDialogBinding.inflate(inflater, container, false);
        initView();
        return mBinding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    public void setContent(@NonNull String content) {
        mContent = content;
        mBinding.tvContent.setText(mContent);
    }

    private void initView() {
        mBinding.tvContent.setText(mContent);
        mBinding.cancelButton.setOnClickListener(v -> {
            mOnConfirmListener.onConfirm(false);
            dismiss();
        });
        mBinding.okButton.setOnClickListener(v -> {
            dismiss();
            if (mOnConfirmListener != null) {
                mOnConfirmListener.onConfirm(true);
            }
        });
    }


    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        mOnConfirmListener = onConfirmListener;
    }

    public interface OnConfirmListener {
        void onConfirm(boolean isOk);
    }

}
